<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<form *ngIf="providerForm" [formGroup]="providerForm" (ngSubmit)="onSubmit()" gioFormFocusInvalid>
  <h1>
    <gio-go-back-button routerLink="../"></gio-go-back-button>
    {{ updateMode ? 'Update client registration provider' : 'New client registration provider' }}
  </h1>

  <mat-card class="client-registration-provider__card">
    <mat-card-content class="client-registration-provider__card__content">
      <h2>General</h2>

      <mat-form-field appearance="outline" class="client-registration-provider__card__content__form-field">
        <input type="text" matInput formControlName="name" autofocus />
        <mat-label>Name</mat-label>
        <mat-hint>Client registration provider name.</mat-hint>
        <mat-error *ngIf="providerForm.get('name').hasError('required')">This field is required.</mat-error>
        <mat-error *ngIf="providerForm.get('name').hasError('minlength')">Name has to be more than 3 characters long</mat-error>
        <mat-error *ngIf="providerForm.get('name').hasError('maxlength')">Name has to be less than 50 characters long.</mat-error>
      </mat-form-field>

      <mat-form-field appearance="outline" class="client-registration-provider__card__content__form-field">
        <input type="text" matInput formControlName="description" />
        <mat-label>Description</mat-label>
        <mat-hint>Provide a description to the client registration provider.</mat-hint>
      </mat-form-field>
    </mat-card-content>
  </mat-card>

  <mat-card class="client-registration-provider__card">
    <mat-card-content class="client-registration-provider__card__content">
      <h2>Configuration</h2>
      <strong>OpenID Connect - Dynamic Client Registration</strong>

      <mat-form-field appearance="outline" class="client-registration-provider__card__content__form-field">
        <mat-label>OpenID Connect Discovery Endpoint</mat-label>
        <input type="url" matInput formControlName="discovery_endpoint" />
      </mat-form-field>

      <mat-form-field appearance="outline" class="client-registration-provider__card__content__form-field">
        <mat-label>Initial Access Token Provider</mat-label>
        <mat-select formControlName="initial_access_token_type">
          <mat-option *ngFor="let initialAccessTokenType of initialAccessTokenTypes" [value]="initialAccessTokenType.value">{{
            initialAccessTokenType.name
          }}</mat-option>
        </mat-select>
        <mat-hint>Define the way the initial access token must be provided.</mat-hint>
      </mat-form-field>

      <ng-container *ngIf="isClientCredentials()">
        <mat-form-field appearance="outline" class="client-registration-provider__card__content__form-field">
          <mat-label>Client ID</mat-label>
          <input type="text" matInput formControlName="client_id" required />
        </mat-form-field>

        <mat-form-field appearance="outline" class="client-registration-provider__card__content__form-field">
          <mat-label>Client Secret</mat-label>
          <input type="text" matInput formControlName="client_secret" required />
        </mat-form-field>

        <mat-form-field appearance="outline" class="client-registration-provider__card__content__form-field">
          <mat-label>Scopes</mat-label>
          <gio-form-tags-input placeholder="Enter a scope and press Enter" formControlName="scopes"></gio-form-tags-input>
        </mat-form-field>

        <mat-form-field appearance="outline" class="client-registration-provider__card__content__form-field">
          <mat-label>Client Template (software_id)</mat-label>
          <input type="text" matInput formControlName="software_id" />
        </mat-form-field>
      </ng-container>

      <ng-container *ngIf="isInitialAccessToken()">
        <mat-form-field appearance="outline" class="client-registration-provider__card__content__form-field">
          <mat-label>Initial Access Token</mat-label>
          <input type="text" matInput formControlName="initial_access_token" required />
        </mat-form-field>
      </ng-container>

      <h3>Renew client_secret (outside DCR specification)</h3>

      <gio-form-slide-toggle>
        <gio-form-label> Enable renew client_secret support </gio-form-label>
        <mat-slide-toggle
          gioFormSlideToggle
          formControlName="renew_client_secret_support"
          aria-label="Renew client secret support"
        ></mat-slide-toggle>
      </gio-form-slide-toggle>

      <mat-form-field appearance="outline" class="client-registration-provider__card__content__row__form-field">
        <mat-label>HTTP Method</mat-label>
        <mat-select formControlName="renew_client_secret_method">
          <mat-option *ngFor="let method of renewClientSecretMethods" [value]="method">{{ method }}</mat-option>
        </mat-select>
        <mat-hint>Define the HTTP method to call the renew secret endpoint.</mat-hint>
      </mat-form-field>

      <mat-form-field appearance="outline" class="client-registration-provider__card__content__row__form-field">
        <mat-label>Endpoint</mat-label>
        <input type="url" matInput formControlName="renew_client_secret_endpoint" />
        <mat-hint>
          Provide an URL to reach the renew client secret endpoint, for example:
          {{ renewClientSecretEndpointUrlExample }}
        </mat-hint>
      </mat-form-field>
    </mat-card-content>
  </mat-card>

  <gio-save-bar
    *gioPermission="{ anyOf: ['environment-client_registration_provider-c', 'environment-client_registration_provider-u'] }"
    [creationMode]="!updateMode"
    [form]="providerForm"
    [formInitialValues]="formInitialValues"
  >
  </gio-save-bar>
</form>
